package com.example.wishstar.viewmodel;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.databinding.ObservableField;

import com.example.wishstar.MainActivity;
import com.example.wishstar.MyApplication;
import com.example.wishstar.db.UserDao;
import com.example.wishstar.db.UserDatabase;

import com.example.wishstar.model.User;
import com.example.wishstar.repository.UserRepository;
import com.example.wishstar.utils.ApiResponse;
import com.example.wishstar.view.activity.RegisterActivity;
import com.example.wishstar.view.activity.ResetActivity;

import retrofit2.Response;

public class LoginViewModel implements UserRepository.LoginCallBack {
    private ObservableField<User> loginObservableField;
    private Context context;
    private UserRepository userRepository;
    public LoginViewModel(Context context) {
        this.context = context;
        User users = new User(0, "", "", 0);
        loginObservableField = new ObservableField<>();
        loginObservableField.set(users);

        UserDatabase userDatabase = MyApplication.getUserDatabase();
        UserDao userDao = userDatabase.userDao();
        userRepository = new UserRepository(userDao, MyApplication.getApi());
        //userRepository.is_login(this);
    }

    public String getUserMail() {
        return loginObservableField.get().user_mail;
    }

    public void setUserMail(String userMail) {
        loginObservableField.get().user_mail = userMail;
    }

    public String getPassword() {
        return loginObservableField.get().password;
    }

    public void setPassword(String password) {
        loginObservableField.get().password = password;
    }

    public void login(View view) {
        userRepository.login(getUserMail(), getPassword(), this);
    }

    public void reset(View view) {
        Intent intent = new Intent(context, ResetActivity.class);
        context.startActivity(intent);
        Activity activity = MyApplication.findActivity(context);
        activity.finish();
    }

    public void register(View view) {
        Intent intent = new Intent(context, RegisterActivity.class);
        context.startActivity(intent);
        Activity activity = MyApplication.findActivity(context);
        activity.finish();
    }

    @Override
    public void callback(Response<ApiResponse<Integer>> response) {
        Toast.makeText(context, response.body().getMessage(), Toast.LENGTH_SHORT).show();
        if(response.body().code == 200) {
            Bundle bundle = new Bundle();
            bundle.putInt("userId", response.body().getData());
            Intent intent = new Intent(context, MainActivity.class);
            intent.putExtras(bundle);
            context.startActivity(intent);
            Activity activity = MyApplication.findActivity(context);
            activity.finish();
        }
    }

    @Override
    public void throwablecallback(Throwable t) {
        Toast.makeText(context, t.getMessage(), Toast.LENGTH_SHORT).show();
    }
}
